#!/usr/bin/env python
# -*- coding: utf-8 -*-
#code:utf-8
#
# Copyright(C) 2011 SupDo.com
# Licensed under the GUN License, Version 3.0 (the "License");
# 
# File:          catalogView.py
# Author:      KuKei
# Create Date: 2012-03-04
# Description: 全局管理视图
# Modify Date: 2012-03-04

from sd.cms.views.admin import AdminBaseView
from sd.cms.lib import Params, Result
from sd.cms.forms.paramForm import GlobalForm, GlobalParamForm
from sd.cms.models.paramModel import Global, GlobalParam
from django.http import HttpResponseRedirect as HRedirect
from sd.cms.lib.decorator import powerLevel

class GlobalView(AdminBaseView):
    @powerLevel(Params.INITIATOR)
    def edit(self, **args):
        globalForm = GlobalForm()
        if self.request.POST:
            globalForm = GlobalForm(self)
            if globalForm.is_valid():
                formData = globalForm.cleaned_data
                objId = int(self.request.POST['_id'])
                model = Global().initData(formData)
                model.id = objId if objId > -1 else None; 
                model.save()
                self.mc.flushNS()
                self.result = Result(True,'操作成功！','','/admin/Global.html',2)
            else:
                self.result = Result(False,globalForm.GetErrors(),'','/admin/Global.html',3)
        if self.request.POST.has_key('_retType') and self.request.POST['_retType'] == 'json':
            return self.render_json()
        else:
            return self.view()
        
    @powerLevel(Params.INITIATOR)
    def addParam(self, **args):
        paramForm = GlobalParamForm()
        if self.request.POST:
            paramForm = GlobalParamForm(self)
            if paramForm.is_valid():
                formData = paramForm.cleaned_data
                paramModel = GlobalParam().initData(formData)
                paramModel.issys = -1;
                paramModel.save()
                self.mc.flushNS2(self.params.GLOBAL)
                self.mc.flushBase(self.params.GLOBAL)
                self.result = Result(True,'操作成功！','','/admin/paramGlobal/'+str(formData['kind'])+'.html',2)
            else:
                self.result = Result(False,paramForm.GetErrors(),'','',3)
        return self.render_json()
    
    @powerLevel(Params.INITIATOR)
    def editParam(self, **args):
        paramForm = GlobalParamForm()
        if self.request.POST:
            paramForm = GlobalParamForm(self)
            if paramForm.is_valid():
                formData = paramForm.cleaned_data
                objId = int(self.request.POST['_id'])
                if objId > -1:
                    oldParam = GlobalParam.objects.get(id=objId)
                    paramModel = GlobalParam().initData(formData)
                    paramModel.id = objId
                    if not oldParam.issys == -1:
                        paramModel.code = oldParam.code
                        paramModel.name = oldParam.name
                        paramModel.issys = oldParam.issys
                    paramModel.save()
                    self.mc.flushNS2(self.params.GLOBAL)
                    self.mc.flushBase(self.params.GLOBAL)
                    self.result = Result(True,'操作成功！','','/admin/paramGlobal/'+str(formData['kind'])+'.html',2)
                else:
                    self.result = Result(False,'没有对象ID','','',3)
            else:
                self.result = Result(False,paramForm.GetErrors(),'','',3)
        return self.render_json()
    
    @powerLevel(Params.INITIATOR)
    def delParam(self, **args):
        if self.request.POST:
            paramModel = GlobalParam.objects.get(id=int(args['id']))
            if paramModel.kind == -1:
                paramModel.delete()
                self.mc.flushNS2(self.params.GLOBAL)
                self.result.flag = True
                self.result.msg = "删除成功！"
            else:
                self.result.flag = False
                self.result.msg = "必要参数不允许删除！"
        return self.render_json()
        
    def view(self, **args):
        globalModelList = list(self.getGlobal())
        globalModel = globalModelList[0] if len(globalModelList) > 0 else None
        globalForm = GlobalForm(self, modelData=globalModel)
        self.viewData['globalForm'] = globalForm
        return self.render('/admin/global.html')
    
    def paramView(self, **args):
        paramModels = list(GlobalParam.objects.filter(kind=args['kind']).order_by('orderby'))
        paramList = []
        for param in paramModels:
            myParamForm = GlobalParamForm(self,modelData=param)
            myParamForm.fields['value'].widget.attrs['class'] = 'textarea'+str(param.style)
            paramList.append(myParamForm)
        paramForm = GlobalParamForm()
        paramForm.fields['issys'].initial = -1;
        paramForm.fields['kind'].initial = int(args['kind'])
        self.viewData['paramList'] = paramList
        self.viewData['paramForm'] = paramForm
        return self.render('/admin/globalParam.html')
    
    def detailView(self, **args):
        param = GlobalParam.objects.get(id=int(args['id']))
        paramForm = GlobalParamForm(self,modelData=param)
        paramForm.fields['value'].widget.attrs['class'] = 'textarea'+str(param.style)
        self.viewData["etype"] = 'param'
        self.viewData["e"] = paramForm
        return self.render('/admin/include/detail.html')
    
def action(request, **args):
    return apply(GlobalView(request).action, (), args)